package com.yorick.logic.service;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.yorick.logic.entity.Account;
import com.yorick.logic.protobuf.Audio;
import com.yorick.netty.WebSocketServerHandler;
import com.yorick.netty.messageListener.LogicService;
import com.yorick.netty.utils.ServerUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.EmptyByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.util.AttributeKey;

import java.nio.ByteBuffer;
import java.util.Objects;

public class AudioServiceImpl implements LogicService {

    @Override
    public void task(byte[] data, ChannelHandlerContext ctx) throws InvalidProtocolBufferException {
        String account = ctx.channel().attr(AttributeKey.valueOf("account")).get().toString();
        Audio.C2SAudio audio = Audio.C2SAudio.parseFrom(data);
        if(!Account.audios.containsKey(account)){
            ByteBufAllocator a1=ctx.alloc();
            ByteBuf buf=a1.directBuffer(1024);
            Account.audios.put(account,buf);
        }
        Account.audios.get(account).writeBytes(audio.getAudio().toByteArray());
    }
}
